#include<iostream>
#include<algorithm>
#include<cstring>
#include<map>

using namespace std;
const int N = 210, INF = 1e9 + 10;

int k, n, m, s, e;

int g[N][N];

void f(int a[][N], int b[][N], int c[][N]){
    static int temp[N][N];
    for(int i = 1; i <= n; i ++){
        for(int j = 1; j <= n; j ++){
            temp[i][j] = INF;
        }
    }

    for(int k = 1; k <= n; k ++){
        for(int i = 1; i <= n; i ++){
            for(int j = 1; j <= n; j ++){
                temp[i][j] = min(temp[i][j], b[i][k] + c[k][j]);
            }
        }
    }

    memcpy(a, temp, sizeof temp);
}

void qmi(){
    int res[N][N];

    for(int i = 1; i <= n; i ++){
        for(int j = 1; j <= n; j ++){
            if(i == j){
                res[i][j] = 0;
            } else{
                res[i][j] = INF;
            }
        }
    }

    while(k){
        if(k & 1) f(res, res, g);
        f(g, g, g);
        k >>= 1;
    }

    printf("%d", res[s][e]);
}

int main(){
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);

    map<int, int> mp;

    //如果初始化时候 i==j特判为0的化那么也就是将自己走到自己也算一条边，事实上是不算的
    for(int i = 1; i < N; i ++){
        for(int j = 1; j < N; j ++){
            g[i][j] = INF;
        }
    }

    scanf("%d%d%d%d", &k, &m, &s, &e);


    mp[s] = ++ n;
    mp[e] = ++ n;
    s = mp[s];
    e = mp[e];
    for(int i = 0; i < m; i ++){
        int a, b, c;
        scanf("%d%d%d", &c, &a, &b);
        if(mp.count(a) == 0) mp[a] = ++n;
        if(mp.count(b) == 0) mp[b] = ++n;

        int x = mp[a], y = mp[b];
        g[y][x] = g[x][y] = min(g[x][y], c);
        
    }

    qmi();

    return 0;

}